home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp® 2.0.1 Tutorial / Chapter 13 / UIconEdit.inc1.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  14.7 KB  |  555 lines  |  [TEXT/MPS ]

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.     
  14.     { Resource identifiers }
  15.     
  16.     kSeedIconId =        1000;                        { Id of the seed icon resource.            }
  17.  
  18.     kIconWindowId =        1000;                        { Id of the icon window 'view' resource.}
  19.     kIconViewId =        1001;                        { Id of the TIconView resource.            }
  20.  
  21.     
  22.     { Constants for TIconView }
  23.  
  24.     kDefaultMagnification =    7;                        { Default icon magnification.            }
  25.     kBorder =                5;                        { Border in which to inset drawing.        }
  26.  
  27.     { Command Numbers }
  28.  
  29.     cZoomIn =            1000;                        { Zoom In menu command.                    }
  30.     cZoomOut =            1001;                        { Zoom Out menu command.                }
  31.     cInvert =            1002;                        { Invert menu command.                    }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. TYPE
  38.     LongArrayHdl =        ^LongArrayPtr;
  39.     LongArrayPtr =        ^LongArray;
  40.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  41.  
  42.  
  43.  
  44. {-------------------------------------------------------------------------------------------}
  45. {--------------------------------TIconApplication methods-----------------------------------}
  46. {-------------------------------------------------------------------------------------------}
  47.  
  48. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  49.  
  50. VAR anIconView : TIconView;
  51.  
  52. BEGIN
  53.     IApplication(iconFileType);
  54.     
  55.     if gCreateWithTemplates then begin  { Make sure the linker doesn't strip out view code. }
  56.         New(anIconView);
  57.     end;
  58. END;
  59.  
  60.  
  61.  
  62. {-------------------------------------------------------------------------------------------}
  63.  
  64. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  65.  
  66. VAR
  67.     anIconDocument:        TIconDocument;
  68.  
  69. BEGIN
  70.     New(anIconDocument);                            { Create a TIconDocument object.        }
  71.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  72.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  73.  
  74.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  75. END;
  76.     
  77.     
  78.     
  79.     
  80. {-------------------------------------------------------------------------------------------}
  81. {----------------------------------TIconDocument methods------------------------------------}
  82. {-------------------------------------------------------------------------------------------}
  83.  
  84. PROCEDURE TIconDocument.IIconDocument;
  85.  
  86. VAR anIconBitMap : TIconBitMap;
  87.  
  88. BEGIN
  89.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  90.                                                     { fails, TIconDocument.Free works okay.    }
  91.  
  92.     IDocument(kFileType,                             { This document's file type.            }
  93.               kSignature,                             { This document's creator.                }
  94.               kUsesDataFork,                         { This document does use the data fork    }
  95.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  96.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  97.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  98.  
  99.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  100.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  101.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  102.  
  103.     fIconBitMap := anIconBitMap;                    { Store a reference to it in a field.    }
  104. END;
  105.  
  106.  
  107.  
  108. {-------------------------------------------------------------------------------------------}
  109.  
  110. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  111.     { This method is called to set the document's data to the "new" state, as when the user    }
  112.     { chooses to open a new document instead of an existing one.  We set the value of the     }
  113.     { document's icon bit map to that of a "seed" icon in our resource file.  That way we     }
  114.     { can the document's initial state simply by changing the "seed" icon resource.            }
  115.  
  116. VAR
  117.     seedIcon:        Handle;
  118.  
  119. BEGIN
  120.     seedIcon := GetIcon(kSeedIconId);                { Get the seed icon resource.            }
  121.     FailNilResource(seedIcon);
  122.     fIconBitMap.SetIconBitMap(seedIcon)
  123. END;
  124.  
  125.  
  126.  
  127. {-------------------------------------------------------------------------------------------}
  128.  
  129. PROCEDURE TIconDocument.Free; OVERRIDE;
  130.     
  131. BEGIN
  132.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  133.  
  134.     INHERITED Free;
  135. END;
  136.  
  137.  
  138.  
  139. {-------------------------------------------------------------------------------------------}
  140.  
  141. PROCEDURE TIconDocument.DoMakeViews (forPrinting: BOOLEAN); OVERRIDE;
  142.  
  143. VAR
  144.     aWindow:  TWindow;
  145.  
  146. BEGIN
  147.     aWindow := NewTemplateWindow(kIconWindowId, SELF);        { Create the view hierarchy }
  148. END;
  149.  
  150.  
  151.  
  152. {-------------------------------------------------------------------------------------------}
  153.  
  154. PROCEDURE TIconDocument.RedrawViews;
  155.  
  156.     PROCEDURE InvalidateView (aView: TView);
  157.  
  158.     BEGIN
  159.         aView.ForceRedraw;                            { Cause the view to be redrawn.            }
  160.     END;
  161.  
  162. BEGIN
  163.     ForAllViewsDo(InvalidateView);                    { Call InvalidateView on each of         }
  164.                                                     { …the document's views.                }
  165. END;
  166.  
  167.  
  168. {-------------------------------------------------------------------------------------------}
  169.  
  170. PROCEDURE TIconDocument.DoSetupMenus; OVERRIDE;
  171.  
  172. BEGIN
  173.     INHERITED DoSetupMenus;                            { Set up inherited menus.                }
  174.  
  175.     Enable(cInvert, TRUE);                            { The icon can always be inverted.        }
  176. END;
  177.  
  178.  
  179.  
  180. {-------------------------------------------------------------------------------------------}
  181.  
  182. FUNCTION TIconDocument.DoMenuCommand (aCmdNumber: CmdNumber): TCommand; OVERRIDE;
  183.  
  184. VAR
  185.     anInvertCommand:        TInvertCommand;
  186.  
  187. BEGIN
  188.     CASE aCmdNumber OF
  189.         cInvert:
  190.             BEGIN
  191.             New(anInvertCommand);
  192.             FailNIL(anInvertCommand);
  193.             anInvertCommand.IInvertCommand(SELF);
  194.             DoMenuCommand := anInvertCommand;
  195.             END;
  196.         OTHERWISE
  197.             DoMenuCommand := INHERITED DoMenuCommand(aCmdNumber);
  198.         END;
  199. END;
  200.  
  201.  
  202.  
  203. {-------------------------------------------------------------------------------------------}
  204.  
  205. PROCEDURE TIconDocument.InvertIcon;
  206.  
  207. BEGIN
  208.     fIconBitMap.Invert;                                { Invert the bits of the icon.            }
  209.     RedrawViews;                                    { Make sure all views get redrawn.        }
  210. END;
  211.  
  212.  
  213.     
  214. {-------------------------------------------------------------------------------------------}
  215.  
  216. PROCEDURE TIconDocument.Fields (PROCEDURE DoToField (fieldName: Str255;
  217.                                                    fieldAddr: Ptr;
  218.                                                    fieldType: INTEGER)); OVERRIDE;
  219.  
  220. BEGIN
  221.     DoToField('TIconDocument', NIL, bClass);
  222.     DoToField('fIconBitMap', @fIconBitMap, bObject);
  223.  
  224.     INHERITED Fields(DoToField);
  225. END;
  226.  
  227.  
  228. {-------------------------------------------------------------------------------------------}
  229. {------------------------------------TIconView methods--------------------------------------}
  230. {-------------------------------------------------------------------------------------------}
  231.  
  232.  
  233. PROCEDURE TIconView.IRes (itsDocument: TDocument; itsSuperView: TView; VAR itsParams: Ptr);
  234.  
  235. BEGIN
  236.     INHERITED IRes(itsDocument, itsSuperView, itsParams);
  237.  
  238.     fMagnification := kDefaultMagnification;
  239.     fIconDocument := TIconDocument(itsDocument);
  240. END;
  241.  
  242.  
  243.  
  244. {-------------------------------------------------------------------------------------------}
  245.  
  246. PROCEDURE TIconView.CalcMinSize (VAR minSize: VPoint); OVERRIDE;
  247.  
  248. BEGIN
  249.     minSize.h := kIconHBits * fMagnification + kBorder + kBorder;
  250.     minSize.v := kIconVBits * fMagnification + kBorder + kBorder;
  251. END;
  252.  
  253.  
  254.  
  255.  
  256. {-------------------------------------------------------------------------------------------}
  257.  
  258. PROCEDURE TIconView.Draw (area: Rect); OVERRIDE;
  259.  
  260. VAR
  261.     drawingRect:    Rect;
  262.  
  263. BEGIN
  264.     SetRect(drawingRect, kBorder, kBorder,
  265.                          kBorder + (kIconHBits * fMagnification),
  266.                          kBorder + (kIconVBits * fMagnification));
  267.  
  268.     fIconDocument.fIconBitMap.Draw(drawingRect);    
  269. END;
  270.  
  271.  
  272. {-------------------------------------------------------------------------------------------}
  273.  
  274. PROCEDURE TIconView.DoSetupMenus; OVERRIDE;
  275.  
  276. BEGIN
  277.     INHERITED DoSetupMenus;                            { Set up inherited menus.                }
  278.  
  279.     Enable(cZoomIn, TRUE);                            { Can always zoom in.                    }
  280.     Enable(cZoomOut, fMagnification > 1);            { Can zoom out if not at smallest size.    }
  281. END;
  282.  
  283.  
  284.  
  285. {-------------------------------------------------------------------------------------------}
  286.  
  287. FUNCTION TIconView.DoMenuCommand (aCmdNumber: CmdNumber): TCommand; OVERRIDE;
  288.  
  289. BEGIN
  290.     DoMenuCommand := gNoChanges;                    { Initialize result of DoMenuCommand.    }
  291.  
  292.     CASE aCmdNumber OF                                { Decide if this command is ours…        }
  293.  
  294.         cZoomIn:
  295.             SetMagnification(fMagnification + 2);    { Increase size of each bit by 2 pixels.}
  296.  
  297.         cZoomOut:
  298.             SetMagnification(fMagnification - 2);    { Decrease size of each bit by 2 pixels.}
  299.  
  300.         OTHERWISE                                    { Otherwise, let someone else handle it.}
  301.             DoMenuCommand := INHERITED DoMenuCommand(aCmdNumber);
  302.     END;
  303. END;
  304.  
  305.  
  306.  
  307. {-------------------------------------------------------------------------------------------}
  308.  
  309. PROCEDURE TIconView.SetMagnification (magnification: INTEGER);
  310.  
  311. BEGIN
  312.     fMagnification := Max(1, magnification);        { Set the new magnification.            }
  313.     AdjustSize;                                        { Magnification affects the view's size.}
  314.     ForceRedraw;                                    { Force the view to be entirely redrawn.}
  315. END;
  316.  
  317.  
  318.  
  319.  
  320. {-------------------------------------------------------------------------------------------}
  321.  
  322. PROCEDURE TIconView.Fields (PROCEDURE DoToField (fieldName: Str255;
  323.                                                  fieldAddr: Ptr;
  324.                                                  fieldType: INTEGER)); OVERRIDE;
  325.                                                  
  326. BEGIN
  327.     DoToField('TIconView', NIL, bClass);
  328.     DoToField('fIconDocument', @fIconDocument, bObject);
  329.     DoToField('fMagnification', @fMagnification, bInteger);
  330.     
  331.     INHERITED Fields(DoToFIeld);
  332. END;
  333.  
  334.  
  335.  
  336.  
  337.  
  338. {-------------------------------------------------------------------------------------------}
  339. {----------------------------------TInvertCommand methods-----------------------------------}
  340. {-------------------------------------------------------------------------------------------}
  341.  
  342. PROCEDURE TInvertCommand.IInvertCommand (itsIconDocument: TIconDocument);
  343.  
  344. BEGIN
  345.     ICommand(cInvert, itsIconDocument,                { Initialize the inherited data…        }
  346.              NIL, NIL);                                { …no view or scroller to track.        }
  347.  
  348.     fIconDocument := itsIconDocument;                { Save a reference to the icon document.}
  349. END;
  350.  
  351.  
  352.  
  353. {-------------------------------------------------------------------------------------------}
  354.  
  355. PROCEDURE TInvertCommand.DoIt; OVERRIDE;
  356.  
  357. BEGIN
  358.     fIconDocument.InvertIcon;                        { Invert the document's icon bitmap.    }
  359. END;
  360.  
  361.  
  362.  
  363. {-------------------------------------------------------------------------------------------}
  364.  
  365. PROCEDURE TInvertCommand.UndoIt; OVERRIDE;
  366.  
  367. BEGIN
  368.     fIconDocument.InvertIcon;                        { Uninvert the document's icon bitmap.    }
  369. END;
  370.  
  371.  
  372.  
  373. {-------------------------------------------------------------------------------------------}
  374.  
  375. PROCEDURE TInvertCommand.RedoIt; OVERRIDE;
  376.         
  377. BEGIN
  378.     fIconDocument.InvertIcon;                        { Reinvert the document's icon bitmap.    }
  379. END;
  380.  
  381.  
  382.  
  383. {-------------------------------------------------------------------------------------------}
  384. {-----------------------------------TIconBitMap methods-------------------------------------}
  385. {-------------------------------------------------------------------------------------------}
  386.  
  387. PROCEDURE TIconBitMap.IIconBitMap;
  388.  
  389. BEGIN
  390.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  391.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  392. END;
  393.  
  394.  
  395.  
  396. {-------------------------------------------------------------------------------------------}
  397.  
  398. PROCEDURE TIconBitMap.Free; OVERRIDE;
  399.  
  400. BEGIN
  401.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  402. END;
  403.  
  404.  
  405.  
  406. {-------------------------------------------------------------------------------------------}
  407.  
  408. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  409.  
  410. BEGIN
  411.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  412.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  413. END;
  414.             
  415.             
  416.  
  417. {-------------------------------------------------------------------------------------------}
  418.  
  419. PROCEDURE TIconBitMap.Clear;
  420.  
  421. VAR
  422.     iconAsLongArray:    LongArrayHdl;
  423.     i:                    INTEGER;
  424.  
  425. BEGIN
  426.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  427.  
  428.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  429.         iconAsLongArray^^[i] := 0;
  430. END;
  431.  
  432.  
  433.  
  434. {-------------------------------------------------------------------------------------------}
  435.  
  436. PROCEDURE TIconBitMap.Invert;
  437.  
  438. VAR
  439.     iconAsLongArray:    LongArrayHdl;
  440.     i:                    INTEGER;
  441.  
  442. BEGIN
  443.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  444.  
  445.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  446.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  447. END;
  448.  
  449.  
  450.  
  451. {-------------------------------------------------------------------------------------------}
  452.  
  453. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  454.     { This converts the given icon bit to a word and bit in an array of long words. }
  455.  
  456. VAR
  457.     bitNumber:        INTEGER;
  458.  
  459. BEGIN
  460.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  461.     word := bitNumber DIV 32;
  462.     bit := 31 - (bitNumber MOD 32);
  463. END;
  464.  
  465.  
  466.  
  467. {-------------------------------------------------------------------------------------------}
  468.  
  469. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  470.     { Returns the state of the given bit in the icon being drawn. }
  471.  
  472. VAR
  473.     word:            INTEGER;
  474.     bitInWord:        INTEGER;
  475.  
  476. BEGIN
  477.     IconBitToWordBit(iconBit, word, bitInWord);
  478.  
  479.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  480. END;
  481.  
  482.  
  483.  
  484. {-------------------------------------------------------------------------------------------}
  485.  
  486. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  487.     { Set the state of the given bit in the icon being drawn. }
  488.  
  489. VAR
  490.     word:            INTEGER;
  491.     bitInWord:        INTEGER;
  492.  
  493. BEGIN
  494.     IconBitToWordBit(iconBit, word, bitInWord);
  495.  
  496.     {$H-}                                            { So the compiler thinks this is unsafe.}
  497.     IF turnBitOn THEN
  498.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  499.     ELSE
  500.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  501.     {$H+}
  502. END;
  503.  
  504.  
  505.  
  506. {-------------------------------------------------------------------------------------------}
  507.  
  508. FUNCTION TIconBitMap.Copy: TIconBitMap;
  509.  
  510. VAR
  511.     copyOfIcon:        TIconBitMap;
  512.  
  513. BEGIN
  514.     New(copyOfIcon);                                { Create a TIcon object.                }
  515.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  516.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  517.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  518.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  519. END;
  520.  
  521.  
  522.             
  523. {-------------------------------------------------------------------------------------------}
  524.  
  525. PROCEDURE TIconBitMap.Draw (area: Rect);
  526.  
  527. BEGIN
  528.     PlotIcon(area, fDataHandle);    
  529. END;
  530.  
  531.  
  532.  
  533. {-------------------------------------------------------------------------------------------}
  534.  
  535. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  536.  
  537. BEGIN
  538.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  539. END;
  540.  
  541.  
  542. {-------------------------------------------------------------------------------------------}
  543.  
  544. PROCEDURE TIconBitMap.Fields (PROCEDURE DoToField (fieldName: Str255;
  545.                                                    fieldAddr: Ptr;
  546.                                                    fieldType: INTEGER)); OVERRIDE;
  547.  
  548. BEGIN
  549.     DoToField('TIconBitMap', NIL, bClass);
  550.     DoToField('fDataHandle', @fDataHandle, bHandle);
  551.  
  552.     INHERITED Fields(DoToField);
  553. END;
  554.  
  555.